package com.newer.textdbnotepad.sdk

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.ResponseBody
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path
import javax.inject.Singleton

interface ApiInterface {
    companion object {
        const val BASE_URL = "https://textdb.online/"
    }

    /**获取记录-内部使用*/
    @Suppress("FunctionName")
    @GET("/{key}")
    suspend fun _getRecord(@Path("key") recordKey: String): ResponseBody

    /**获取记录*/
    suspend fun getRecord(recordKey: String): String{
        return _getRecord(recordKey).string()
    }

    /**创建, 更新记录*/
    @POST("/update/?v=1")
    @FormUrlEncoded
    suspend fun updateRecord(@Field("key") recordKey: String, @Field("value") value: String): UpdateResponse

}

@Module
@InstallIn(SingletonComponent::class)
class ApiModule {

    @Provides
    @Singleton
    fun provideApi(): ApiInterface {
        return RetrofitClient.newClient(ApiInterface.BASE_URL).create(ApiInterface::class.java)
    }

}
